﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Crevice2D.Core.Data;
using Crevice2D.Core.Managers;
using Box2DX.Collision;
using Patyk.Core;
using Patyk.Data;

namespace Patyk.Game
{
    public class Level
    {
        private Background[] _layers;
        private Terrain _terrain;
        private Map _map;

        // The layer which entities are drawn on top of.
        private const int EntityLayer = 1;

        public Player Player
        {
            get { return _player; }
        }
        private Player _player;

        #region Loading

        public Level(int levelIndex)
        {
            //load game entities
            _map = new Map();
            _terrain = new Terrain(_map);
            _player = new Player();

            //TODO: load start position for level from file (?)
            var playerPosition = new Vector2(0, 0);

            Player.Initialize(playerPosition, ConstantStrings.CannonTexture, ConstantStrings.TankTexture);

            // Load background layer textures. For now, all levels must
            // use the same backgrounds and only use the left-most part of them.
            _layers = new Background[1];
            var mainLayer = new StaticBackground();
            mainLayer.Initialize(ConstantStrings.MainBackground, 0);
            _layers[0] = mainLayer;

            // Load sounds.

        }

        #endregion

        #region Update

        /// <summary>
        /// Updates all objects in the world, performs collision between them,
        /// and handles the time limit with scoring.
        /// </summary>
        public void Update(
            GameTime gameTime,
            KeyboardState currKeyboardState, KeyboardState prevKeyboardState,
            GamePadState currGamePadState, GamePadState prevGamePadState,
            MouseState currMouseState, MouseState prevMouseState)
        {
            foreach(var layer in _layers)
            {
                layer.Update(gameTime);
            }

            _player.Update(gameTime, currKeyboardState, prevKeyboardState, currGamePadState, prevGamePadState, currMouseState);
            HandleCollisions();
        }

        #endregion

        #region Draw

        /// <summary>
        /// Draw everything in the level from background to foreground.
        /// </summary>
        public void Draw(GameTime gameTime)
        {
            //for (int i = 0; i < EntityLayer; ++i)
            //    _layers[i].Draw();

            _terrain.Draw(gameTime);
            _player.Draw(gameTime);

            //for (int i = EntityLayer + 1; i < _layers.Length; ++i)
            //    _layers[i].Draw();
        }

        #endregion

        private void HandleCollisions()
        {
            Collision.PerPixelCollide(_player.Sprite, _player.Transformation, _terrain.Sprite, _terrain.Transformation);
        }
    }
}
